package com.lb.service.impl;

import com.lb.entity.TbMovie;
import com.lb.dao.TbMovieDao;
import com.lb.service.TbMovieService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TbMovie)表服务实现类
 *
 * @author libo
 * @since 2024-09-09 14:09:15
 */
@Service("tbMovieService")
public class TbMovieServiceImpl implements TbMovieService {
    @Resource
    private TbMovieDao tbMovieDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbMovie queryById(Integer id) {
        return this.tbMovieDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param tbMovie     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<TbMovie> queryByPage(TbMovie tbMovie, PageRequest pageRequest) {
        long total = this.tbMovieDao.count(tbMovie);
        return new PageImpl<>(this.tbMovieDao.queryAllByLimit(tbMovie, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tbMovie 实例对象
     * @return 实例对象
     */
    @Override
    public TbMovie insert(TbMovie tbMovie) {
        this.tbMovieDao.insert(tbMovie);
        return tbMovie;
    }

    /**
     * 修改数据
     *
     * @param tbMovie 实例对象
     * @return 实例对象
     */
    @Override
    public TbMovie update(TbMovie tbMovie) {
        this.tbMovieDao.update(tbMovie);
        return this.queryById(tbMovie.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbMovieDao.deleteById(id) > 0;
    }

    @Override
    public ResponseEntity<List<TbMovie>> queryAll() {
        return ResponseEntity.ok(tbMovieDao.queryList());
    }
}
